package com.xr.sunnyday.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.sunnyday.dao.CheckitemDao;
import com.xr.sunnyday.entity.PageResult;
import com.xr.sunnyday.entity.QueryPageBean;
import com.xr.sunnyday.exception.SunnydayException;
import com.xr.sunnyday.pojo.CheckItem;
import com.xr.sunnyday.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service(interfaceClass = CheckitemService.class)
public class CheckitemServiceImp implements CheckitemService {

    @Autowired
    private CheckitemDao checkitemDao;

    /*public void setCheckgroupDao(CheckitemDao checkitemDao) {
        this.checkitemDao = checkitemDao;
    }*/


    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        /*
        * 分页的模糊查询
        * */
        Page<CheckItem> page = checkitemDao.findByCoundition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);
    }

    @Override
    public void deleteById(int id) throws SunnydayException{
        int count = checkitemDao.findCountCheckItemId(id);
        if(count>0){
            throw new SunnydayException("检查项关联了检查组!");
        }
        checkitemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(int id) {
        return checkitemDao.findById(id);
    }

    @Override
    public void update(CheckItem checkItem) {
        checkitemDao.update(checkItem);
    }
}
